﻿Imports MindFusion.Diagramming
Imports MindFusion.Drawing

Public Class ClsMMContainerNode
    Inherits ContainerNode
    'represents my own diagram container

#Region "Constructors"
    Public Sub New()
        MyBase.new()
        MMSetDefaultProperties()
    End Sub

    Public Sub New(ByVal prototype As MindFusion.Diagramming.ContainerNode)
        MyBase.New()
    End Sub

    Public Sub New(ByVal parent As MindFusion.Diagramming.Diagram)
        MyBase.New(parent)
        MMSetDefaultProperties()
    End Sub

    Protected Sub New(ByVal prototype As MindFusion.Diagramming.DiagramItem)
        MyBase.New(prototype)
    End Sub

    Protected Sub New(ByVal prototype As MindFusion.Diagramming.DiagramNode)
        MyBase.New(prototype)
    End Sub
#End Region
#Region "Subs"
#Region "Overrides & Overloads Subs"
    'overrided sub for drawing a container
    Public Overrides Sub Draw(ByVal graphics As MindFusion.Drawing.IGraphics, ByVal options As MindFusion.Diagramming.RenderOptions)
        MyBase.Draw(graphics, options)
        'drawing triangle for note indication

        DrawNoteTriangle(Me, graphics)
    End Sub
    'overrided sub for drawing handles
    Public Overrides Sub DrawHandles(ByVal graphics As MindFusion.Drawing.IGraphics, ByVal color As System.Drawing.Color)
        'Debug.Print("DrawHandles")
        Const LdbWidthOfLine As Double = 0.7
        Dim LoColor As Color

        If Me.Parent.ActiveItem IsNot Nothing Then
            If Me.Parent.ActiveItem.Equals(Me) Then
                'me is active selected
                'Debug.Print("DrawHandles-SELECTED")
                LoColor = Drawing.Color.Blue
            Else
                'me is not active selected
                'Debug.Print("DrawHandles-NOT SELECTED")
                LoColor = Drawing.Color.Gray
            End If
        Else
            'nothing is selected
            'Debug.Print("DrawHandles-NOTHING IS SELECTED")
            LoColor = Drawing.Color.Gray
        End If

        'drawing selection rectangle
        graphics.DrawRectangle(SDPenVirginCreator(LoColor, LdbWidthOfLine), CType(Me.Bounds.X - LdbWidthOfLine / 2, Single), CType(Me.Bounds.Y - LdbWidthOfLine / 2, Single), CType(Me.Bounds.Width + LdbWidthOfLine, Single), CType(Me.Bounds.Height + LdbWidthOfLine, Single))
    End Sub
    'overrided sub called after deserialization of node. I can set some properties forcible here!
    Protected Overrides Sub OnLoad()
        MyBase.OnLoad()
    End Sub
#End Region
#Region "MM Subs"
    'default settings of shape
    Public Sub MMSetDefaultProperties()
        Me.Constraints.MinHeight = 20
        Me.Constraints.MinWidth = 20
        Me.Obstacle = True ' whether this node is considered an obstacle by the link-routing algorithm
        Me.Bounds = SDRectangleVirginCreator(0, 0, 50, 50)
        Me.Font = New Font("Arial", 8, FontStyle.Regular) 'font used when rendering the container's texts
        Me.AllowIncomingLinks = True 'true - because of notes
        Me.AllowOutgoingLinks = True 'true - because of notes
        Me.Brush = New LinearGradientBrush(Color.White, Color.LightBlue, 45) 'brush is used to fill the interiors of geometric shapes
        Me.Pen = New Pen("0/#FF696969/0/0/0//0/0/10/") 'pen is used to draw the outline of geometric shapes
        Me.HandlesStyle = HandlesStyle.EasyMove 'visual styles for frames that are drawn around selected nodes
        Me.ShadowOffsetX = 0.5 'horizontal offset of the shadow
        Me.ShadowOffsetY = 0.5 'vertical offset of the shadow
        Me.CaptionHeight = 6 'height of the container's caption area
        Me.Margin = 2 'how much space should be left between contained nodes and the container borders
        Me.AutoShrink = True 'whether the container should shrink in size when nodes are removed from it
        Me.Foldable = False 'whether the user is allowed to fold this container
        Me.HighlightPen = New Pen(Color.BlueViolet) 'pen used to draw the container frame lines when an item is dragged over the container
        Me.CaptionFormat.Alignment = StringAlignment.Center  'specifies the alignment of an object relative to its container
        Me.CaptionFormat.FormatFlags = StringFormatFlags.NoClip
        Me.CaptionFormat.Trimming = StringTrimming.None
        Me.Caption = GsStringDefaultValue 'the text displayed as caption of the container
    End Sub
    'resizes height of caption and bounds
    Public Sub MMResizeAllToFitContainer()
        MMResizeHeightOfCaption()
        UpdateBounds()
    End Sub
    'resizes heights of caption
    Public Sub MMResizeHeightOfCaption()
        Dim LnNewHeight As Single
        Dim LnWidth As Single

        'checking Me.Parent for MeasureUnit
        If Me.Parent Is Nothing Then
            Debug.Print("CHYBA!!! - MMResizeHeightOfCaption - Me.Parent = Nothing")
        End If

        'resizing height of caption
        'maxWidth parameter for MeasureString sub = actual width of caption
        LnWidth = Me.Bounds.Width

        LnNewHeight = Me.Parent.MeasureString(Me.Caption, Me.Font, LnWidth, Me.CaptionFormat).Height

        'setting a new height or keeping minimal height of caption
        Const LnMinimalCaptionHeight As Single = 6
        Me.CaptionHeight = IIf(LnNewHeight > LnMinimalCaptionHeight, LnNewHeight, LnMinimalCaptionHeight)

        'height of container will be at minimum as height of caption + any constant (10 :-))
        Me.Constraints.MinHeight = Me.CaptionHeight + 10
        'need to set new height right now?
        If Me.Bounds.Height < Me.Constraints.MinHeight Then
            Me.Resize(Me.Bounds.Width, Me.Constraints.MinHeight)
        End If
    End Sub
    'adding node programmatically to container
    Public Sub MMAddNodeToContainerProgrammatically(ByVal OoNode As DiagramNode)
        'add node to container
        Me.Add(OoNode)
    End Sub
#End Region
#Region "Functions"
#Region "Overrides & Overloads Functions"
    'overrided sub - when a diagram item is being dragged over this node
    Public Overrides Function OnDragOver(ByVal item As MindFusion.Diagramming.DiagramItem) As Boolean
        'only table is accepted for adding to container
        If MMAllowAddNodeToContainer(item) Then
            Return MyBase.OnDragOver(item)
        Else
            Return False
        End If
    End Function
    'overrided sub - when a diagram item is dropped over this node
    Public Overrides Function OnDropOver(ByVal item As MindFusion.Diagramming.DiagramItem) As Boolean
        'only table is accepted for adding to container
        If MMAllowAddNodeToContainer(item) Then
            Return MyBase.OnDropOver(item)
        Else
            Return False
        End If
    End Function
#End Region
#Region "MM Functions"
    'examine possibility for adding node to container
    Private Function MMAllowAddNodeToContainer(ByVal IoItem As DiagramItem) As Boolean
        'only table is accepted for adding to container
        Return IoItem.GetType.Name = "ClsMMTableNode"
    End Function
#End Region
#End Region
#End Region

End Class
